﻿using System;
using System.Collections.Generic;
using System.Text;
using BF.IBLL.Dictionary;
using BF.Infrastructure;
using BF.IDAL.Dictionary;
using BF.Model;
using System.Linq.Expressions;
using BF.BLL.BaseSystem;

namespace BF.BLL.Dictionary
{
    public class Dic_OrganizationBLL : Idic_OrganizationBLL
    {
        private readonly Idic_OrganizationDAL iDal;
        private readonly IUnitOfWork<WSJD_BaseFrameworkEntities> uwork;
        public Dic_OrganizationBLL(IUnitOfWork<WSJD_BaseFrameworkEntities> uwork, Idic_OrganizationDAL iDal)
        {
            this.iDal = iDal;
            this.uwork = uwork;
        }

        public bool Add(Model.dic_Organization entity)
        {
            iDal.Add(entity);
            int count = uwork.Commit();
            if (count > 0)
                return true;
            else
                return false;
        }

        public bool Add(List<dic_Organization> list)
        {
            foreach (var item in list)
            {
                iDal.Add(item);
            }
            int count = uwork.Commit();
            if (count > 0)
                return true;
            else
                return false;
        }

        //public bool Add(List<dic_Hospital> sysAreaList, List<dic_Hospital> updateList)
        //{
        //    foreach (var item in sysAreaList)
        //    {
        //        iDal.Add(item);
        //    }
        //    foreach (var item in updateList)
        //    {
                
        //    }
        //    int count = uwork.Commit();
        //    if (count > 0)
        //        return true;
        //    else
        //        return false;
        //}

        public bool Update()
        {
            int count = uwork.Commit();
            if (count > 0)
                return true;
            else
                return false;
        }

        public bool Delete(Model.dic_Organization entity)
        {
            iDal.Delete(entity);
            int count = uwork.Commit();
            if (count > 0)
                return true;
            else
                return false;
        }

        public bool Delete(System.Linq.Expressions.Expression<Func<Model.dic_Organization, bool>> predicate)
        {
            iDal.Delete(predicate);
            int count = uwork.Commit();
            if (count > 0)
                return true;
            else
                return false;
        }

        public List<dic_Organization> GetList(Expression<Func<dic_Organization, bool>> where, Expression<Func<dic_Organization, object>> orderBy, PageModel pageModel, bool isAscending)
        {
            return iDal.GetList(where, orderBy, pageModel, isAscending);
        }
        public List<dic_Organization> GetList(Expression<Func<dic_Organization, bool>> where)
        {
            return iDal.GetList(where);
        }


        public int GetRowCount(Expression<Func<dic_Organization, bool>> where)
        {
            return iDal.GetRowCount(where);
        }

        public dic_Organization GetModel(Guid gID)
        {
            return iDal.GetModelByCondition(P => P.gKey.Equals(gID));
        }

        public dic_Organization GetModel(Guid? gID, string sCode)
        {
            if (gID == null)
            {
                return iDal.GetModelByCondition(p => p.sOrganizationCode == sCode);
            }
            else
            {
                return iDal.GetModelByCondition(p => p.sOrganizationCode == sCode && p.gKey != gID);
            }
        }

        public dic_Organization GetModelByCondition(Expression<Func<dic_Organization, bool>> where)
        {
            return iDal.GetModelByCondition(where);
        }

        /// <summary>
        ///获取上级机构编码,仅限卫生室
        /// </summary>
        /// <returns></returns>
        public List<dic_Organization> GetHigherCode()
        {
            return iDal.GetHigherCode();
        }
    }
}
